package controller.login;

import core.util.ValidationUtil;
import domain.entity.base.UserInfo;
import domain.service.base.UserInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import util.WebContextUtil;

/**
 * Created by bowen on 2016/7/28.
 */
@Controller
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping(value = "/statisticalInfor")
    public String statisticalInfor(String code) {
        logger.info("===============statisticalInfor=====================" + code + "=========================================");
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.isAuthenticated();
        boolean admin = false;
        try {
            UserInfo userInfo =  ValidationUtil.getUserInfoByCode(code);
            UsernamePasswordToken token = new UsernamePasswordToken(userInfo.getUserid(), userInfo.getUserid());
            //subject权限对象
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            if (userInfo != null) {
                session.setAttribute("user", userInfo);
                admin = userInfoService.getAdmin(userInfo.getUserid());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(admin){
            return "index";
        } else {
            return "statisticalInfor";
        }
    }


    @RequestMapping(value = "/adminInfor")
    public String adminInfor() {
            return "manage-member";
    }

    @RequestMapping(value = "/listInfor")
    public String listInfor() {
        return "statisticalInfor";
    }


    /**
     * admin配置初始化数据
     *
     * @return
     */
    @RequestMapping(value = "checkUserIsAdmin")
    @ResponseBody
    public boolean getAdminChck() {
        String userId = WebContextUtil.getCurrentUser().getUserid();
        return userInfoService.getAdmin(userId);
    }

    @RequestMapping("toUserData")
    public String toUserData(){
        return "manage-member";
    }

    @RequestMapping("toStaticData")
    public String toStaticData(){
        return "manage-content";
    }

    @RequestMapping("mangageLog")
    public String mangageLog(){
        return "manage-log";
    }
}
